<?php

namespace App\Admin\Controllers;

use App;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Models\TestingDeveloper;
use TestingDeveloperService;
use App\Services\APIUtil;
use App\Services\DeveloperHelper;

class TestingDeveloperController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.testing_developer.title.list'));
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header(trans('backend.testing_developer.title.edit'));
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header(trans('backend.testing_developer.title.add'));
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(TestingDeveloper::class, function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');
            $grid->name(trans('backend.testing_developer.column.name'));
            $grid->abbreviation(trans('backend.testing_developer.column.abbreviation'));
            $grid->email(trans('backend.testing_developer.column.email'));
            $grid->mobile(trans('backend.testing_developer.column.mobile'));
            $grid->appid(trans('backend.testing_developer.column.appid'));
            $grid->appsecret(trans('backend.testing_developer.column.appsecret'));
            $grid->union_code(trans('backend.testing_developer.column.union_code'));
            $grid->enable(trans('backend.testing_developer.column.enable'));
            $grid->disableExport();
        });
    }



    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(TestingDeveloper::class, function (Form $form) {
            $form->text("name",trans('backend.testing_developer.column.name'))->rules('required');
            $form->text("abbreviation",trans('backend.testing_developer.column.abbreviation'))->rules('required|max:26');
            $form->email('email',trans('backend.testing_developer.column.email'))->rules('required|email');
            $form->mobile("mobile",trans('backend.testing_developer.column.mobile'))->rules('required');
            $form->password("password",trans('backend.testing_developer.column.password'))->rules('required|min:6');
            $form->hidden('appid');
            $form->hidden('appsecret');
            $form->hidden('union_code');
            $form->saving(function(Form $form) {
                if($form->password && $form->model()->password != $form->password)
                {
                    $form->password = bcrypt($form->password);
                }
            });
        });
    }

    public function options()
    {
        return TestingDeveloperService::getResultOptions();
    }
}
